﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports System.Data.Common


Public Class RolesToUsers
#Region "Constants"
    Protected Const _rtuID As String = "rtuID"
    Protected Const _RoleID As String = "RoleID"
    Protected Const _UserID As String = "UserID"
#End Region
#Region "Private"
    Private Shared intrtuID As Integer
    Private Shared intRoleID As Integer
    Private Shared intUserID As Integer

    'We are setting bool values on what roles users are.
    Private Shared blSysAdmin As Boolean = False
    Private Shared blManagement As Boolean = False
    Private Shared blEmployee As Boolean = False

    Private Shared el As New errorLog
#End Region
#Region "Public"
    Public Property isSysAdmin() As Boolean
        Get
            Return blSysAdmin
        End Get
        Set(ByVal value As Boolean)
            blSysAdmin = value
        End Set
    End Property
    Public Property isManagement() As Boolean
        Get
            Return blManagement
        End Get
        Set(ByVal value As Boolean)
            blManagement = value
        End Set
    End Property
    Public Property isEmployee() As Boolean
        Get
            Return blEmployee
        End Get
        Set(ByVal value As Boolean)
            blEmployee = value
        End Set
    End Property
    Public Property rtuID() As Integer
        Get
            Return intrtuID
        End Get
        Set(ByVal value As Integer)
            intrtuID = value
        End Set
    End Property
    Public Property RoleID() As Integer
        Get
            Return intRoleID
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property
    Public Property UserID() As Integer
        Get
            Return intUserID
        End Get
        Set(ByVal value As Integer)
            intUserID = value
        End Set
    End Property
#End Region
    Public Function GetRolesToUsersByUserID() As Boolean
        Dim _blFoundRoles = False
        Dim db As Database = DbStoreFactory.CreateDatabase
        Dim cmd As DbCommand = DirectCast(Nothing, DbCommand)
        Dim reader As IDataReader = Nothing

        Try
            cmd = db.GetStoredProcCommand("GetRolesToUsersByUserID")
            db.DiscoverParameters(cmd)
            cmd.Parameters("@intUserID").Value = intUserID

            reader = db.ExecuteReader(cmd)

            Do While (reader.Read)
                If (reader(_RoleID) = 1) Then
                    blSysAdmin = True
                    _blFoundRoles = blSysAdmin
                ElseIf (reader(_RoleID) = 2) Then
                    blManagement = True
                    _blFoundRoles = blManagement
                ElseIf (reader(_RoleID) = 3) Then
                    blEmployee = True
                    _blFoundRoles = blEmployee
                End If
            Loop

        Catch ex As Exception
            el.CatchError(ex)
        Finally
            If Not (cmd Is Nothing) Then
                cmd.Dispose()
            End If
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
        Return _blFoundRoles
    End Function

End Class
